/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package knn.iris;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Nguyen Thuy Ngoc <ntngoc1988@gmail.com>
 */
public class IrisSampleLoader {

    public static List<IrisSample> load(String filename) {
        List<IrisSample> samples = new ArrayList<IrisSample>();

        try {
            FileInputStream fstream = new FileInputStream(filename);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            while ((strLine = br.readLine()) != null) {
                String[] items = strLine.split(",");
                assert (items.length == 5);

                double sepalLength = Double.valueOf(items[0]);
                double sepalWidth = Double.valueOf(items[1]);
                double petalLength = Double.valueOf(items[2]);
                double petalWidth = Double.valueOf(items[3]);
                String assignedClass = items[4];

                IrisSample sample = create(sepalLength, sepalWidth, petalLength, petalWidth, assignedClass);
                samples.add(sample);
            }

            in.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
        return samples;
    }
    
    

    private static IrisSample create(double sepalLength,
            double sepalWidth,
            double petalLength,
            double petalWidth,
            String assignedClass) {

        IrisSample sample = new IrisSample();

        sample.setSepalLength(sepalLength);
        sample.setSepalWidth(sepalWidth);
        sample.setPetalLength(petalLength);
        sample.setPetalWidth(petalWidth);
        sample.setAssignedClass(assignedClass);

        return sample;
    }
}
